package com.a.l;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import static android.content.Context.BIND_AUTO_CREATE;

public class JwtServerDomain {
    private Context mContext;
    private static JwtServerDomain sJwtServerDomain;
    private boolean mInitSucess;
    private String Tag = "JwtServerDomain";
    private Thread mThread;
    private static final long DETAULT = 12 * 60 * 60;
    private long mTime = DETAULT;

    private JwtServerDomain(Context context) {
        mContext = context;
    }

    public static JwtServerDomain getInstance(Context context) {
        if (sJwtServerDomain == null) {
            sJwtServerDomain = new JwtServerDomain(context);
        }
        return sJwtServerDomain;
    }

    public boolean init() {
        Intent intent = new Intent(IKonkeAidlInterface.class.getName());
        intent.setAction("konkeServer");
//        intent.setPackage("com.a.l");com.ikonke.huarun_pad_app  com.ikonke.huarun_pad_app

        intent.setComponent(new ComponentName("com.ikonke.huarun_pad_app", "com.a.l.KonkeServer"));
        boolean b = mContext.bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
        Log.e("1", b + "");
        mInitSucess = b;
        return b;
    }

    public void update() {
        if (mInitSucess) {
            try {
                mIKonkeAidlInterface.pushToken();
            } catch (RemoteException e) {
                e.printStackTrace();
                Log.e(Tag, "RemoteException : " + e.getMessage());
            }
            if (mThread != null) {
                mTime = DETAULT;
            } else {
                mThread = new Thread(() -> {
                    while (true) {
                        try {
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        mTime = mTime - 1;
                        if (mTime > 0) {
                            continue;
                        }
                        if (mIKonkeAidlInterface != null) {
                            try {
                                mIKonkeAidlInterface.pushToken();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        mTime = DETAULT;
                    }
                });
                mThread.start();
            }
        } else {
            init();
        }
    }

    public void putStringToSp(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(
                "FlutterSharedPreferences",
                Context.MODE_MULTI_PROCESS);
        sp.edit().putString(key, value).commit();
    }

    public String getStringFormSp(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(
                "FlutterSharedPreferences",
                Context.MODE_MULTI_PROCESS);
        return sp.getString(key, "");
    }


    private IKonkeAidlInterface mIKonkeAidlInterface;

    private ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.e(Tag, "onServiceConnected");
            mIKonkeAidlInterface = IKonkeAidlInterface.Stub.asInterface(iBinder);
            update();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(Tag, "onServiceDisconnected");
        }
    };
}
